<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.2/css/all.min.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css">

<script class="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"littlefxc.github.io","root":"/","images":"/images","scheme":"Mist","version":"8.2.2","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}};
  </script>
<meta name="description" content="1 Consul 单机集群搭建本文是在生产环境中Consul服务端节点不稳定后，导致注册到Consul集群上的部分服务不可用，从而造成开放能力平台无法使用的背景下产生的。 目的是探讨一个高可用Consul集群方案。 1.1 consul 架构Server负责组成 cluster 的复杂工作（选举、状态维护、转发请求到 lead），以及 consul 提供的服务（响应 RCP 请求）。考虑到容错和收">
<meta property="og:type" content="article">
<meta property="og:title" content="Consul 单机集群搭建">
<meta property="og:url" content="http://littlefxc.github.io/2021/01/11/Consul-%E5%8D%95%E6%9C%BA%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA/index.html">
<meta property="og:site_name" content="一年春又来">
<meta property="og:description" content="1 Consul 单机集群搭建本文是在生产环境中Consul服务端节点不稳定后，导致注册到Consul集群上的部分服务不可用，从而造成开放能力平台无法使用的背景下产生的。 目的是探讨一个高可用Consul集群方案。 1.1 consul 架构Server负责组成 cluster 的复杂工作（选举、状态维护、转发请求到 lead），以及 consul 提供的服务（响应 RCP 请求）。考虑到容错和收">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/consul%E6%9E%B6%E6%9E%841.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20210112101929745.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20210112102147403.png">
<meta property="og:image" content="https://gitee.com/littlefxc/oss/raw/master/images/image-20210112102223434.png">
<meta property="article:published_time" content="2021-01-11T08:41:37.000Z">
<meta property="article:modified_time" content="2021-03-25T13:15:48.941Z">
<meta property="article:author" content="一年春又来">
<meta property="article:tag" content="consul">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/littlefxc/oss/raw/master/images/consul%E6%9E%B6%E6%9E%841.png">


<link rel="canonical" href="http://littlefxc.github.io/2021/01/11/Consul-%E5%8D%95%E6%9C%BA%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA/">


<script class="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>
<title>Consul 单机集群搭建 | 一年春又来</title>
  




  <noscript>
  <style>
  body { margin-top: 2rem; }

  .use-motion .menu-item,
  .use-motion .sidebar,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header {
    visibility: visible;
  }

  .use-motion .header,
  .use-motion .site-brand-container .toggle,
  .use-motion .footer { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle,
  .use-motion .custom-logo-image {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line {
    transform: scaleX(1);
  }

  .search-pop-overlay, .sidebar-nav { display: none; }
  .sidebar-panel { display: block; }
  </style>
</noscript>

<link rel="alternate" href="/atom.xml" title="一年春又来" type="application/atom+xml">
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">一年春又来</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/" rel="section"><i class="home                          //首页 fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="archive          //归档 fa-fw"></i>归档</a></li>
        <li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="th           //分类 fa-fw"></i>分类</a></li>
        <li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="tags                     //标签 fa-fw"></i>标签</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#1-Consul-%E5%8D%95%E6%9C%BA%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA"><span class="nav-text">1 Consul 单机集群搭建</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#1-1-consul-%E6%9E%B6%E6%9E%84"><span class="nav-text">1.1 consul 架构</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-2-Consul-%E8%8A%82%E7%82%B9%E8%A7%84%E5%88%92"><span class="nav-text">1.2 Consul 节点规划</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-3-Consul-%E6%9C%8D%E5%8A%A1%E7%AB%AF%E8%8A%82%E7%82%B9%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E6%A8%A1%E7%89%88"><span class="nav-text">1.3 Consul 服务端节点配置文件模版</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-4-Consul-%E5%AE%A2%E6%88%B7%E7%AB%AF%E8%8A%82%E7%82%B9%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6%E6%A8%A1%E7%89%88"><span class="nav-text">1.4 Consul 客户端节点配置文件模版</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#1-5-%E6%90%AD%E5%BB%BA%E9%9B%86%E7%BE%A4"><span class="nav-text">1.5 搭建集群</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#2-Consul-%E7%9A%84%E4%B8%80%E4%BA%9B%E5%91%BD%E4%BB%A4"><span class="nav-text">2 Consul 的一些命令</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#2-1-%E6%9F%A5%E7%9C%8B-consul-%E9%9B%86%E7%BE%A4%E5%88%97%E8%A1%A8"><span class="nav-text">2.1 查看 consul 集群列表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-2-%E6%9F%A5%E7%9C%8B-Consul-%E8%8A%82%E7%82%B9%E5%88%97%E8%A1%A8"><span class="nav-text">2.2 查看 Consul 节点列表</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#2-3-%E7%A7%BB%E9%99%A4%E4%B8%80%E4%B8%AA-Server-%E8%8A%82%E7%82%B9"><span class="nav-text">2.3 移除一个 Server 节点</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#3-%E9%AA%8C%E8%AF%81"><span class="nav-text">3 验证</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#3-1-%E6%A8%A1%E6%8B%9F%E4%B8%80%E4%B8%AA-Consul-%E6%9C%8D%E5%8A%A1%E7%AB%AF%E5%AE%95%E6%9C%BA"><span class="nav-text">3.1 模拟一个 Consul 服务端宕机</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#4-%E7%BB%93%E8%AE%BA"><span class="nav-text">4 结论</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#5-%E5%8F%82%E8%80%83%E8%B5%84%E6%BA%90"><span class="nav-text">5 参考资源</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">一年春又来</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/archives/">
        
          <span class="site-state-item-count">234</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/categories/">
          
        <span class="site-state-item-count">38</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/tags/">
          
        <span class="site-state-item-count">125</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://littlefxc.github.io/2021/01/11/Consul-%E5%8D%95%E6%9C%BA%E9%9B%86%E7%BE%A4%E6%90%AD%E5%BB%BA/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="一年春又来">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="一年春又来">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          Consul 单机集群搭建
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2021-01-11 16:41:37" itemprop="dateCreated datePublished" datetime="2021-01-11T16:41:37+08:00">2021-01-11</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2021-03-25 21:15:48" itemprop="dateModified" datetime="2021-03-25T21:15:48+08:00">2021-03-25</time>
      </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E6%B3%A8%E5%86%8C%E4%B8%AD%E5%BF%83/" itemprop="url" rel="index"><span itemprop="name">注册中心</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="1-Consul-单机集群搭建"><a href="#1-Consul-单机集群搭建" class="headerlink" title="1 Consul 单机集群搭建"></a>1 Consul 单机集群搭建</h1><p>本文是在生产环境中Consul服务端节点不稳定后，导致注册到Consul集群上的部分服务不可用，从而造成开放能力平台无法使用的背景下产生的。</p>
<p>目的是探讨一个高可用Consul集群方案。</p>
<h2 id="1-1-consul-架构"><a href="#1-1-consul-架构" class="headerlink" title="1.1 consul 架构"></a>1.1 consul 架构</h2><p>Server负责组成 cluster 的复杂工作（选举、状态维护、转发请求到 lead），以及 consul 提供的服务（响应 RCP 请求）。考虑到容错和收敛，一般部署 3 ~ 5 个比较合适，而client数量不做限制，架构如下:</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/consul%E6%9E%B6%E6%9E%841.png" alt="consul 架构"></p>
<h2 id="1-2-Consul-节点规划"><a href="#1-2-Consul-节点规划" class="headerlink" title="1.2 Consul 节点规划"></a>1.2 Consul 节点规划</h2><table>
<thead>
<tr>
<th><strong>节点名称</strong></th>
<th>节点类型</th>
<th><strong>HTTP端口</strong></th>
<th><strong>DNS端口</strong></th>
<th><strong>serf_lan端口</strong></th>
<th><strong>serf_wan端口</strong></th>
</tr>
</thead>
<tbody><tr>
<td>consul-server-1</td>
<td>server</td>
<td>8501</td>
<td>8601</td>
<td>8001</td>
<td>8002</td>
</tr>
<tr>
<td>consul-server-2</td>
<td>server</td>
<td>8502</td>
<td>8602</td>
<td>8101</td>
<td>8102</td>
</tr>
<tr>
<td>consul-server-3</td>
<td>server</td>
<td>8503</td>
<td>8603</td>
<td>8201</td>
<td>8202</td>
</tr>
<tr>
<td>consul-client-1</td>
<td>client</td>
<td>8504</td>
<td>8604</td>
<td>8301</td>
<td>8302</td>
</tr>
<tr>
<td>consul-client-2</td>
<td>client</td>
<td>8505</td>
<td>8605</td>
<td>8401</td>
<td>8402</td>
</tr>
</tbody></table>
<h2 id="1-3-Consul-服务端节点配置文件模版"><a href="#1-3-Consul-服务端节点配置文件模版" class="headerlink" title="1.3 Consul 服务端节点配置文件模版"></a>1.3 Consul 服务端节点配置文件模版</h2><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="attr">&quot;bind_addr&quot;</span>: <span class="string">&quot;127.0.0.1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;client_addr&quot;</span>: <span class="string">&quot;127.0.0.1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;ports&quot;</span>: &#123;</span><br><span class="line">        <span class="attr">&quot;http&quot;</span>: <span class="number">8501</span>,</span><br><span class="line">        <span class="attr">&quot;dns&quot;</span>: <span class="number">8601</span>,</span><br><span class="line">        <span class="attr">&quot;serf_lan&quot;</span>: <span class="number">8001</span>,</span><br><span class="line">        <span class="attr">&quot;serf_wan&quot;</span>: <span class="number">8002</span>,</span><br><span class="line">        <span class="attr">&quot;server&quot;</span>: <span class="number">8000</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">&quot;datacenter&quot;</span>: <span class="string">&quot;dc1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;data_dir&quot;</span>: <span class="string">&quot;/Users/fengxuechao/WorkSpace/IdeaProjects/consul/consul-server-1/data&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;log_level&quot;</span>: <span class="string">&quot;INFO&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;log_file&quot;</span>: <span class="string">&quot;/Users/fengxuechao/WorkSpace/IdeaProjects/consul/consul-server-1/log/consul.log&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;node_name&quot;</span>: <span class="string">&quot;consul-server-1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;disable_host_node_id&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">&quot;server&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">&quot;ui&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">&quot;bootstrap_expect&quot;</span>: <span class="number">3</span>,</span><br><span class="line">    <span class="attr">&quot;rejoin_after_leave&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">&quot;retry_join&quot;</span>: [</span><br><span class="line">        <span class="string">&quot;127.0.0.1:8001&quot;</span>,</span><br><span class="line">        <span class="string">&quot;127.0.0.1:8101&quot;</span>,</span><br><span class="line">        <span class="string">&quot;127.0.0.1:8201&quot;</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>注意：</p>
<ul>
<li><strong>disable_host_node_id</strong>：不使用host信息生成node ID，适用于同一台服务器部署多个实例用于测试的情况。随机生成nodeID。</li>
</ul>
<h2 id="1-4-Consul-客户端节点配置文件模版"><a href="#1-4-Consul-客户端节点配置文件模版" class="headerlink" title="1.4 Consul 客户端节点配置文件模版"></a>1.4 Consul 客户端节点配置文件模版</h2><figure class="highlight json"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><span class="line">&#123;</span><br><span class="line">    <span class="attr">&quot;bind_addr&quot;</span>: <span class="string">&quot;127.0.0.1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;client_addr&quot;</span>: <span class="string">&quot;127.0.0.1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;ports&quot;</span>: &#123;</span><br><span class="line">        <span class="attr">&quot;http&quot;</span>: <span class="number">8505</span>,</span><br><span class="line">        <span class="attr">&quot;dns&quot;</span>: <span class="number">8605</span>,</span><br><span class="line">        <span class="attr">&quot;serf_lan&quot;</span>: <span class="number">8401</span>,</span><br><span class="line">        <span class="attr">&quot;serf_wan&quot;</span>: <span class="number">8402</span>,</span><br><span class="line">        <span class="attr">&quot;server&quot;</span>: <span class="number">8400</span></span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="attr">&quot;datacenter&quot;</span>: <span class="string">&quot;dc1&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;data_dir&quot;</span>: <span class="string">&quot;/Users/fengxuechao/WorkSpace/IdeaProjects/consul/consul-client-2/data&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;log_level&quot;</span>: <span class="string">&quot;INFO&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;log_file&quot;</span>: <span class="string">&quot;/Users/fengxuechao/WorkSpace/IdeaProjects/consul/consul-client-2/log/consul.log&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;node_name&quot;</span>: <span class="string">&quot;consul-client-2&quot;</span>,</span><br><span class="line">    <span class="attr">&quot;disable_host_node_id&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">&quot;server&quot;</span>: <span class="literal">false</span>,</span><br><span class="line">    <span class="attr">&quot;ui&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">&quot;rejoin_after_leave&quot;</span>: <span class="literal">true</span>,</span><br><span class="line">    <span class="attr">&quot;retry_join&quot;</span>: [</span><br><span class="line">        <span class="string">&quot;127.0.0.1:8001&quot;</span>,</span><br><span class="line">        <span class="string">&quot;127.0.0.1:8101&quot;</span>,</span><br><span class="line">        <span class="string">&quot;127.0.0.1:8201&quot;</span></span><br><span class="line">    ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h2 id="1-5-搭建集群"><a href="#1-5-搭建集群" class="headerlink" title="1.5 搭建集群"></a>1.5 搭建集群</h2><figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 创建 Consul 服务端节点目录</span></span><br><span class="line">mkdir consul-server-&#123;1,2,3&#125;</span><br><span class="line"><span class="comment"># 创建 Consul 服务端节点可执行文件目录</span></span><br><span class="line">mkdir consul-server-&#123;1,2,3&#125;/bin</span><br><span class="line"><span class="comment"># 创建  Consul 服务端节点配置文件目录</span></span><br><span class="line">mkdir consul-server-&#123;1,2,3&#125;/config</span><br><span class="line"><span class="comment"># 创建  Consul 服务端节点数据文件目录</span></span><br><span class="line">mkdir consul-server-&#123;1,2,3&#125;/data</span><br><span class="line"><span class="comment"># 创建  Consul 服务端节点日志文件目录</span></span><br><span class="line">mkdir consul-server-&#123;1,2,3&#125;/<span class="built_in">log</span></span><br><span class="line"></span><br><span class="line"><span class="comment"># consul 客户端节点目录结构与服务端节点目录结构保持一致</span></span><br><span class="line">...</span><br><span class="line"></span><br><span class="line"><span class="comment"># Consul 服务端节点搭建</span></span><br><span class="line">nohup consul-server-1/bin/consul agent -config-dir=consul-server-1/config &amp;</span><br><span class="line">nohup consul-server-2/bin/consul agent -config-dir=consul-server-2/config &amp;</span><br><span class="line">nohup consul-server-3/bin/consul agent -config-dir=consul-server-3/config &amp;</span><br><span class="line"><span class="comment"># Consul 客户端节点搭建</span></span><br><span class="line">nohup consul-client-1/bin/consul agent -config-dir=consul-client-1/config &amp;</span><br><span class="line">nohup consul-client-2/bin/consul agent -config-dir=consul-client-2/config &amp;</span><br></pre></td></tr></table></figure>

<h1 id="2-Consul-的一些命令"><a href="#2-Consul-的一些命令" class="headerlink" title="2 Consul 的一些命令"></a>2 Consul 的一些命令</h1><h2 id="2-1-查看-consul-集群列表"><a href="#2-1-查看-consul-集群列表" class="headerlink" title="2.1 查看 consul 集群列表"></a>2.1 查看 consul 集群列表</h2><p>验证 Consul 集群是否搭建成功</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">consul-server-1/bin/consul operator raft list-peers -http-addr=127.0.0.1:8501</span><br></pre></td></tr></table></figure>

<h2 id="2-2-查看-Consul-节点列表"><a href="#2-2-查看-Consul-节点列表" class="headerlink" title="2.2 查看 Consul 节点列表"></a>2.2 查看 Consul 节点列表</h2><p>查看 Consul 的所有节点列表</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">consul-server-1/bin/consul members -http-addr=127.0.0.1:8501</span><br></pre></td></tr></table></figure>

<h2 id="2-3-移除一个-Server-节点"><a href="#2-3-移除一个-Server-节点" class="headerlink" title="2.3 移除一个 Server 节点"></a>2.3 移除一个 Server 节点</h2><p>验证 Consul 客户端节点不会丢失</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">consul-server-1/bin/consul leave -http-addr=127.0.0.1:8501</span><br></pre></td></tr></table></figure>

<h1 id="3-验证"><a href="#3-验证" class="headerlink" title="3 验证"></a>3 验证</h1><h2 id="3-1-模拟一个-Consul-服务端宕机"><a href="#3-1-模拟一个-Consul-服务端宕机" class="headerlink" title="3.1 模拟一个 Consul 服务端宕机"></a>3.1 模拟一个 Consul 服务端宕机</h2><ol>
<li>移除 Consul 服务端节点前：</li>
</ol>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210112101929745.png" alt="image-20210112101929745"></p>
<ol start="2">
<li><p>移除 Consul 服务端节点。命令参考章节2.3。</p>
<p>结果如下：</p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210112102147403.png" alt="image-20210112102147403"></p>
<p><img src="https://gitee.com/littlefxc/oss/raw/master/images/image-20210112102223434.png" alt="image-20210112102223434"></p>
</li>
</ol>
<h1 id="4-结论"><a href="#4-结论" class="headerlink" title="4 结论"></a>4 结论</h1><p>说明当一个consul服务端节点宕机，并不会影响 Consul 客户端节点的可用性。</p>
<p>在每个数据中心，client和server是混合的。一般建议有3-5台server。这是基于有故障情况下的可用性和性能之间的权衡结果，因为越多的机器加入达成共识越慢。虽然每个节点的服务注册数量是有上限的，但是并不限制client的数量，它们可以很容易的扩展到数千或者数万台。</p>
<h1 id="5-参考资源"><a href="#5-参考资源" class="headerlink" title="5 参考资源"></a>5 参考资源</h1><p><a target="_blank" rel="noopener" href="https://blog.csdn.net/u014635374/article/details/106313858">https://blog.csdn.net/u014635374/article/details/106313858</a></p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/consul/" rel="tag"># consul</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2021/01/11/Redis%E7%BC%93%E5%AD%98%E8%BF%87%E6%9C%9F%E6%9C%BA%E5%88%B6%E5%92%8C%E5%86%85%E5%AD%98%E6%B7%98%E6%B1%B0%E7%AE%A1%E7%90%86%E6%9C%BA%E5%88%B6/" rel="prev" title="Redis缓存过期机制和内存淘汰管理机制">
                  <i class="fa fa-chevron-left"></i> Redis缓存过期机制和内存淘汰管理机制
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2021/01/11/ES%E4%B9%8B%E6%96%87%E6%A1%A3%E4%B9%90%E8%A7%82%E9%94%81%E6%8E%A7%E5%88%B6-if-seq-no%E4%B8%8Eif-primary-term/" rel="next" title="ES之文档乐观锁控制 if_seq_no与if_primary_term">
                  ES之文档乐观锁控制 if_seq_no与if_primary_term <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>







<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      const activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      const commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">一年春又来</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/mist/" class="theme-link" rel="noopener" target="_blank">NexT.Mist</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js"></script>
<script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  
<script src="/js/local-search.js"></script>






  





</body>
</html>
